<template>
    <div class="app-container"> 
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:menu:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="info"
            plain
            icon="el-icon-sort"
            size="mini"
            @click="toggleExpandAll"
          >展开/折叠</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table
         v-if="refreshTable"
         v-loading="loading"
         :data="menuList"
         row-key="categoryid"
         :default-expand-all="isExpandAll"
         :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
         <el-table-column prop="categoryname" label="菜单名称" :show-overflow-tooltip="true"></el-table-column>
         <el-table-column prop="ordernum" label="排序" width="60"></el-table-column>
         <el-table-column label="是否有效" align="center" prop="isvalid">
            <template #default="scope">
                <span v-if="scope.row.isvalid === 1">是</span>
                <span v-else>否</span>
             </template>
          </el-table-column> 
         <el-table-column label="创建时间" align="center" prop="addtime">
            <template #default="scope">
               <span>{{ parseTime(scope.row.addtime) }}</span>
            </template>
         </el-table-column>
         <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
               <el-button plain type="primary" icon="el-icon-edit" size="mini" @click="handleUpdate(scope.row)">修改</el-button>
               <el-button plain type="primary" icon="el-icon-plus" size="mini" @click="handleAdd(scope.row)">新增</el-button>
               <el-button plain type="primary" icon="el-icon-delete" size="mini" @click="handleDelete(scope.row)">删除</el-button>
            </template>
         </el-table-column>
      </el-table>
  
      <!-- 添加或修改菜单对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
             <el-form-item label="栏目名称" prop="categoryname">
                <el-input v-model="form.categoryname" placeholder="请输入栏目名称" />
             </el-form-item>
             <el-form-item label="排序号" prop="ordernum">
               <el-input-number v-model="form.ordernum" placeholder="请输入排序号" @change="handleChange" />
                <!-- <el-input v-model="form.ordernum" type="number" placeholder="请输入排序号" /> -->
             </el-form-item>
             <el-form-item label="轮播滚动时间" label-width="120px">
               <el-input v-model="form.beizhu" placeholder="请输入轮播滚动时间" style="width:100px;margin-right:10px" />(秒)
                <!-- <el-input v-model="form.ordernum" type="number" placeholder="请输入排序号" /> -->
             </el-form-item>
             <el-form-item label="是否有效">
                <el-radio-group v-model="form.isvalid" class="ml-4">
                    <el-radio :label=1 size="large">是</el-radio>
                    <el-radio :label=0 size="large">否</el-radio>
                </el-radio-group>
             </el-form-item>
          </el-form>
          <template #footer>
             <div class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
             </div>
          </template>
       </el-dialog>
    </div>
  </template>
  
  <script>
  import { getMenu, addMenu, editMenu, delMenu, findoneMenu } from "@/api/yxzx/menu";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  import IconSelect from "@/components/IconSelect";
  
  export default {
    name: "Menu",
    dicts: ['sys_show_hide', 'sys_normal_disable'],
    components: { Treeselect, IconSelect },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 菜单表格树数据
        menuList: [],
        // 菜单树选项
        menuOptions: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 是否展开，默认全部折叠
        isExpandAll: false,
        // 重新渲染表格状态
        refreshTable: true,
        // 查询参数
        queryParams: {
          menuName: undefined,
          visible: undefined
        },
        // 表单参数
        form: {
          ordernum: undefined,
        },
        // data.number = data.number || undefined;
        // 表单校验
        rules: {
          categoryname: [
            { required: true, message: "菜单名称不能为空", trigger: "blur" }
          ],
          ordernum: [
            { required: true, message: "菜单顺序不能为空", trigger: "blur" }
          ]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      // 选择图标
      selected(name) {
        this.form.icon = name;
      },
      /** 查询菜单列表 */
      getList() {
        this.loading = true;
        getMenu().then(response => {
          this.menuList = this.tranListToTreeData(response.resultContent, "0");
          this.loading = false;
        });
      },
      /** 转换菜单数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.menuId,
          label: node.menuName,
          children: node.children
        };
      },
      /** 查询菜单下拉树结构 */
      getTreeselect() {
        getMenu().then(response => {
          this.menuOptions = [];
          const menu = { menuId: 0, menuName: '主类目', children: [] };
          menu.children = this.handleTree(response.data, "parentcategoryid");
          this.menuOptions.push(menu);
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          parentcategoryid: '0',
          categoryname: '',
          ordernum: undefined,
          isvalid: null
        };
        // this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd(row) {
        this.reset();
        if (row != null && row.categoryid) {
          this.form.parentcategoryid = row.categoryid;
        } else {
          this.form.parentcategoryid = '0';
        }
        this.open = true;
        this.title = "添加菜单";
        this.form.ordernum = undefined;
      },
      /** 展开/折叠操作 */
      toggleExpandAll() {
        this.refreshTable = false;
        this.isExpandAll = !this.isExpandAll;
        this.$nextTick(() => {
          this.refreshTable = true;
        });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        findoneMenu(row.categoryid).then(response => {
          this.form = response;
          this.open = true;
          this.title = "修改菜单";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.categoryid != undefined) {
              editMenu(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addMenu(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$modal.confirm('是否确认删除名称为"' + row.categoryname + '"的数据项？').then(function() {
          return delMenu(row.categoryid);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 改变数字 */
      handleChange(val) {
        this.form.ordernum = val
      },
      tranListToTreeData(list, rootValue) {
        var arr = []
        list.forEach(item => {
          if (item.parentcategoryid === rootValue) {
            const children = this.tranListToTreeData(list, item.categoryid)
            if (children.length) {
              item.children = children
            }
            arr.push(item) 
          }
        })
        return arr
      }
    }
  };
  </script>
  